package com.example.zhyq.repair

import androidx.lifecycle.MutableLiveData
import com.example.base.db.SearchDao
import com.example.base.entity.RepairBean
import com.example.base.entity.RepairBeanItem
import com.example.base.entity.SearchBean
import com.example.base.mvvm.BaseViewModel
import com.example.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(val apiService: ApiService,val searchDao: SearchDao): BaseViewModel() {


    val getRepairSuc = MutableLiveData<RepairBean>()

    val getRepairByNoSuc = MutableLiveData<RepairBeanItem>()

    val getRepairByIdSuc = MutableLiveData<RepairBeanItem>()

    val updateRepairSuc = MutableLiveData<RepairBeanItem>()

    val addRepairSuc = MutableLiveData<RepairBeanItem>()

    val deleteRepairSuc = MutableLiveData<Boolean>()


    val insertSearch = MutableLiveData<Long>()

    val getSearchs = MutableLiveData<List<SearchBean>>()

    val deleteSearch = MutableLiveData<Int>()

    fun getRepairs(){
        httpRequest({apiService.getRepairs()},{getRepairSuc.postValue(it)})
    }

    fun getRepairsByUname(repairName:String){
        httpRequest({apiService.getRepairsByUname(repairName)},{getRepairSuc.postValue(it)})
    }

    fun getRepairsByNo(orderNumber:String){
        httpRequest({apiService.getRepairsByNO(orderNumber)},{getRepairByNoSuc.postValue(it)})
    }

    fun getRepairById(id:Int){
        httpRequest({apiService.getRepairById(id)},{getRepairByIdSuc.postValue(it)})
    }

    fun updateRepair(id: Int,beanItem: RepairBeanItem) {
        httpRequest({apiService.updateRepair(id,toBody(beanItem))},{updateRepairSuc.postValue(it)})
    }

    fun addRepair(beanItem: RepairBeanItem){
        httpRequest({apiService.addRepairs(toBody(beanItem))},{addRepairSuc.postValue(it)})
    }

    fun deleteRepair(id:Int) {
        httpRequest({apiService.deleteRepair(id)},{deleteRepairSuc.postValue(it)})
    }



    fun insertSearch(bean : SearchBean) {
        roomRequest({searchDao.addSearch(bean)},{insertSearch.postValue(it)})
    }

    fun getSearch() {
        roomRequest({searchDao.getSearch()},{getSearchs.postValue(it)})
    }

    fun deleteSearch(bean: SearchBean) {
        roomRequest({searchDao.deleteSearch(bean)},{deleteSearch.postValue(it)})
    }
}